package com.six666.medicaldevicetestingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six666.medicaldevicetestingsystem.dto.Personal_Compare_DTO;
import com.six666.medicaldevicetestingsystem.entity.Project;
import com.six666.medicaldevicetestingsystem.entity.ProjectTest;
import com.six666.medicaldevicetestingsystem.mapper.Project_mapper;
import com.six666.medicaldevicetestingsystem.service.Project_Service;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class Project_Service_Impl extends ServiceImpl<Project_mapper, Project> implements Project_Service {
    @Override
    public List<Project> get_all_project() {
        return list();
    }
    @Override
    public List<Project> get_by_keyword(String keyword) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("ProjectName", keyword);
        return list(queryWrapper);
    }
    @Override
    public List<Personal_Compare_DTO> get_my_project(List<ProjectTest> test) {
        List<Personal_Compare_DTO> res = new ArrayList<>();
        for (ProjectTest projectTest : test) {
            Personal_Compare_DTO dto = new Personal_Compare_DTO();
            QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("ProjectID", projectTest.getProjectid());
            dto.setProject(getOne(queryWrapper));
            dto.setProjectTest(projectTest);
            res.add(dto);
        }
        return res;
    }

    @Override
    public List<String> get_all_sn() {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        List<Project>res = list(queryWrapper);
        List<String> sn = new ArrayList<>();
        boolean flag = true;
        for(int i=0; i<res.size(); i++) {
            for (String s : sn) {
                if (res.get(i).getStandardnumber().equals(s)) {
                    flag = false;
                    break;
                }
            }
            if(flag) {
                sn.add(res.get(i).getStandardnumber());
            }
            else {
                i++;
            }

        }
        return sn;
    }

    @Override
    public Project get_by_id(int id) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ProjectID", id);
        return getOne(queryWrapper);
    }


    @Override
    public int gen_project(List<List<String>> data) {
        int res=-1;
        for (int i = 1; i < data.size(); i++) {
            try {
                Project project = new Project();
                project.setProjectname(data.get(i).get(2));
                project.setProjecttime(new Date());
                project.setProjecttype(data.get(i).get(3));
                project.setCategories(data.get(i).get(0));
                project.setStandardnumber(data.get(i).get(4));
                save(project);
                if (res==-2 || res==0){
                    res=0;
                }
                else{
                    res=1;
                }
            }
            catch (Exception e) {
                e.printStackTrace();
                System.out.println("请检查excel格式");
                if(res==-1){
                    res=-2;
                }
                if(res==1){
                    res=0;
                }
            }
        }
        return res;
    }

    @Override
    public String get_standard_by_project_id(int projectId) {
        QueryWrapper<Project> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ProjectID", projectId);
        return getOne(queryWrapper).getStandardnumber();
    }
    @Override
    public boolean save_new_project(String projectName, String projecttype, String categories, String standardnumber) {
        Project project = new Project();
        Date date = new Date();
        project.setProjectname(projectName);
        project.setProjecttype(projecttype);
        project.setCategories(categories);
        project.setProjecttime(date);
        project.setStandardnumber(standardnumber);
        return save(project);
    }
}
